package org.itboys.admin.service;

import org.itboys.admin.entity.SysDictionary;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 * 
 * @author huanglei
 *
 */
@Service
public class SysDictionaryService extends BaseAdminService<SysDictionary, Long>{
	
	private static final long serialVersionUID = 1L;
	
	@Resource(name="adminDS")
	private MongoDataSource ds;

/*	@Resource(name="sysDictionaryLoader")
	private SysDictionaryLoader sysDictionaryLoader;*/
	
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<SysDictionary> getEntityClass() {
		return SysDictionary.class;
	}
	
	/**
	 * 分页查询列表
	 * @return
	 */
	public Page<SysDictionary> findAllDictionaryAndPage(HttpServletRequest request){
		Map<String, Object> param2 = QueryParamUtils.builderQueryMap(request);
		Page<SysDictionary> page = super.page(param2);
		return page;
	}
	
	/**
	 * 获取所有的信息
	 * @return
	 */
	public List<SysDictionary> getAll(){
		List<SysDictionary> list = ds.createQuery(getEntityClass()).filter("isDeleted", 0).asList();
		return list;
	}
	
	/**
	 * 删除  isDeleted标为1
	 * @param id
	 */
	public void deleteDictionary(Long id){
		SysDictionary dictionary = getById(id);
		update(id, "isDeleted",1);
		//sysDictionaryLoader.clearCache(dictionary.getKey());
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	public List<SysDictionary> pageQueryByPage(HttpServletRequest request){
		Map<String, Object> param2 = QueryParamUtils.builderQueryMap(request);
		Page<SysDictionary> page = super.page(param2);
		List<SysDictionary> list = page.getData();
		return list;
	}
	
	/**
	 * 新增
	 * @param dictionary
	 */
	public void insert(SysDictionary dictionary){
		AdminSessionHolder.prepareAdminLoginData(dictionary);
		save(dictionary);
	}

}
